{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "87bb0c59",
   "metadata": {},
   "source": [
    "## Demo notebook for the mcp exchange rate server - used to show usaage of mcp_exchange_rate.py\n",
    "\n",
    "\n",
    "### see the README.md file for instructions on how to get the free api key\n",
    "### uses claude sonnet 4 openapi endpoint =\"https://api.anthropic.com/v1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "576cf81c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import asyncio\n",
    "from agents import Agent, OpenAIChatCompletionsModel, Runner, trace\n",
    "from agents.mcp import MCPServerStdio\n",
    "from openai import AsyncOpenAI\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display\n",
    "\n",
    "\n",
    "\n",
    "load_dotenv(override=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d85c78c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# 1. Configure Claude client \n",
    "# make sure t=you have the antropic key in your .env file\n",
    "claude_client = AsyncOpenAI(\n",
    "    base_url=\"https://api.anthropic.com/v1\",\n",
    "    api_key=os.getenv(\"ANTHROPIC_API_KEY\")\n",
    ")\n",
    "\n",
    "# 2. Create Claude model\n",
    "claude_model = OpenAIChatCompletionsModel(\n",
    "    model=\"claude-sonnet-4-20250514\",\n",
    "    openai_client=claude_client\n",
    ")\n",
    "# MCP server for forex exchange rates\n",
    "exchange_rate_params = {\n",
    "    \"command\": \"uv\", \"args\": [\"run\", \"mcp_exchange_rates.py\"]\n",
    "}\n",
    "mcp_tools=\"None\"\n",
    "\n",
    "print(exchange_rate_params)\n",
    "# MCP server for forex exchange rates call\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7747a2c6",
   "metadata": {},
   "source": [
    "## Run the exchnage rate server to get various cross rates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1914e2ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "try:\n",
    "    async with MCPServerStdio(\n",
    "        params=exchange_rate_params, client_session_timeout_seconds=30) as forex_server:\n",
    "        print(\"✅ Connected to Forex MCP server\")\n",
    "       # 4. Connect to MCP server\n",
    "                \n",
    "        # 5. List available tools\n",
    "        forex_tools = await forex_server.list_tools()\n",
    "        tools = forex_tools\n",
    "        print(f\"�� Available tools: {tools}\")\n",
    "        \n",
    "        instructions=\"\"\"You are a financial research assistant. You have access to usd conversion tools.  \n",
    "        always display information on fx rates in a table format and the date of the exhcnge rate quote\"\"\"\n",
    "        \n",
    "        request =\"provide the latest exchange rate of usd/aud usd/eur, usd/nzd, usd/gbp, usd/can - print the rates in a table format\\\n",
    "                    print the inverse rates of the above rates\"\n",
    "                \n",
    "        # 6. Create Claude agent with MCP server\n",
    "        agent = Agent(\n",
    "            name=\"claude_researcher\",\n",
    "            instructions=instructions,\n",
    "            model=claude_model,\n",
    "            mcp_servers=[forex_server]\n",
    "        )\n",
    "        \n",
    "        print(\"✅ Claude agent created with MCP servers\")\n",
    "        \n",
    "        \n",
    "        # 7. Use the agent with tracing\n",
    "        with trace(\"Claude Web Research\"):\n",
    "            result = await Runner.run(\n",
    "                agent,\n",
    "                request,\n",
    "                max_turns=25\n",
    "                )\n",
    "        \n",
    "        print(\"\\n📊 Claude Agent Response:\")\n",
    "        print(\"=\" * 50)\n",
    "        # print(result.final_output)\n",
    "        display(Markdown(result.final_output))\n",
    "        print(\"=\" * 50)\n",
    "    \n",
    "except Exception as e:\n",
    "    print(f\"❌ Error: {e}\")\n",
    "    import traceback\n",
    "    traceback.print_exc()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b17c96fa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
